package cn.zn.wx.config;

import cn.zn.framework.common.utils.Constant;
import cn.zn.framework.common.utils.HttpContextUtils;
import cn.zn.framework.redis.RedisUtils;
import cn.zn.modules.zzsys.inter.dto.LoginUserDTO;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;

@Component
public class MybatisPlusMetaObjectOldConfig implements MetaObjectHandler {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "version", Integer.class, 0);
        this.strictInsertFill(metaObject, "createTime", Date.class, Calendar.getInstance().getTime());
        this.strictInsertFill(metaObject, "updateTime", Date.class, Calendar.getInstance().getTime());
        if (this.getFieldValByName("sortNum", metaObject) == null) {
            this.strictInsertFill(metaObject, "sortNum", Integer.class, 100);
        }
        if (this.getFieldValByName("isActive", metaObject) == null) {
            this.strictInsertFill(metaObject, "isActive", Boolean.class, true);
        }
        if (this.getFieldValByName("orderNum", metaObject) == null) {
            this.strictInsertFill(metaObject, "orderNum", Integer.class, 100);
        }
        if (this.getFieldValByName("createUserId", metaObject) == null) {
            boolean completed = false;
            HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
            Object token = request.getAttribute("token");
            if (token != null) {
                //获取用户信息
                LoginUserDTO loginUser = redisUtils.hget(Constant.REDIS_LOGIN_USER, token.toString(), LoginUserDTO.class);
                if (loginUser != null) {
                    this.strictInsertFill(metaObject, "createUserId", String.class, loginUser.getSysId());
                    this.strictInsertFill(metaObject, "updateUserId", String.class, loginUser.getSysId());
                    completed = true;
                }
            }
            if (!completed) {
                this.strictInsertFill(metaObject, "createUserId", String.class, "1");
                this.strictInsertFill(metaObject, "updateUserId", String.class, "1");
            }
        }
        // 新结构
        if (this.getFieldValByName("sort", metaObject) == null) {
            this.strictInsertFill(metaObject, "sort", Integer.class, 100);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", Date.class, Calendar.getInstance().getTime());
        if (this.getFieldValByName("updateUserId", metaObject) == null) {
            boolean completed = false;
            HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
            Object token = request.getAttribute("token");
            if (token != null) {
                //获取用户信息
                LoginUserDTO loginUser = redisUtils.hget(Constant.REDIS_LOGIN_USER, token.toString(), LoginUserDTO.class);
                if (loginUser != null) {
                    this.strictUpdateFill(metaObject, "updateUserId", String.class, loginUser.getSysId());
                    completed = true;
                }
            }
            if (!completed) {
                this.strictUpdateFill(metaObject, "updateUserId", String.class, "1");
            }
        }
    }
}
